<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
class PaperQuestions extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index(){
        if($this::$isdelete){
            $map['pq.isdelete']=1;
        }else{
            $map['pq.isdelete']=0;
        }
        $data=input();

        //模糊查询
        if ($data['title']){
            $map['pq.title'] = ['like','%'.$data['title'].'%'];
        }
        //题型
        if ($data['question_type']){
            $map['pq.type'] = $data['question_type'];
        }
        if ($data['rank_name']){
            //从等级表查询数据
            $rank = Db::name('driver_rank')->where('name',$data['rank_name'])->find();
            $map['pq.rank_id'] = $rank['id'];
        }
        $listRows = $this->request->param('numPerPage') ? : config("paginate.list_rows");
        $list = DB('PaperQuestions')
            ->alias('pq')
            ->join('tp_driver_rank dr','dr.id=pq.rank_id','left')
            ->field('pq.*,dr.name as rank_name')
            ->where($map)
            ->order("pq.id desc")
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $this->view->assign('list',$list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        return $this->view->fetch();
    }

    /**
     * 添加前获取等级列表
     * @return mixed
     */
    public function beforeadd()
    {            //获取等级列表
        $rank_list = get_ranks();

        $this->view->assign('rank_list',$rank_list);
    }

    /**
     * 编辑前获取等级列表
     * @return mixed
     */
    public function beforeedit()
    {            //获取等级列表
        $rank_list = get_ranks();

        $this->view->assign('rank_list',$rank_list);
        if(input('type')&&input('id')){

            Db::name("paper_questions_item")->where('question_id',input('id'))->update(array('item_type'=>input('type')));
        }
    }
    /**
     * 永久删除
     */
    public function deleteForever()
    {
        $where["id"] = ["in", input('id')];
        Db::name("paper_questions")->where($where)->delete();

        Db::name("paper_questions_item")->where('question_id','in',input('id'))->delete();
        return ajax_return_adv("删除成功");
    }

    /**
     * 清空回收站
     */
    public function clear()
    {
       $clear_id = Db::name("paper_questions")->where("isdelete",'1')->column('id');
        Db::name("paper_questions")->where("isdelete",'1')->delete();
        Db::name("paper_questions_item")->where('question_id','in',$clear_id)->delete();
        return ajax_return_adv("清空回收站成功");
    }
}
